<?php

namespace App\Http\Controllers\Api\Config;

use App\Http\Controllers\Controller;
use App\Models\Config\ConfigUsage;
use Illuminate\Http\Request;

class ConfigUsageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $usage = ConfigUsage::all();

        return response()->json([
            'success' => false,
            'message' => '返回成功',
            'data' => $usage
        ], 500);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $usage = new ConfigUsage();
        $usage->name = $request->name;
        $usage->save();

        return response()->json([
            'success' => false,
            'message' => 'Sorry, the user cannot be logged out',
            'data' => $usage
        ], 500);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $usage = ConfigUsage::find($id);

        return response()->json([
            'success' => false,
            'message' => 'Sorry, the user cannot be logged out',
            'data' => $usage
        ], 500);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     * PUT/PATCH
     * header中添加Content-Type:application/json; charset=UTF-8 防止中文乱码
     * body中用raw，添加参数（用json）：{"name":"测试煎法2","institution_id":"0"}
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $usage = ConfigUsage::find($id);
        if ($usage)
        {
            $usage->name = $request->name;
            $usage->save();
        }

        return response()->json([
            'success' => false,
            'message' => 'Sorry, the user cannot be logged out',
            'data' => $usage
        ], 500);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $usage = ConfigUsage::find($id)->delete();
        $success = false;
        if ($usage)
        {
            $success = true;
        }

        return response()->json([
            'success' => $success,
            'message' => 'Sorry, the user cannot be logged out',
            'data' => []
        ], 500);
    }
}
